package nckd.wnqjt.tmc.tda.plugin.form;

import com.alibaba.fastjson.JSON;
import kd.bos.algo.DataSet;
import kd.bos.algo.Row;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.entity.qing.*;
import kd.bos.form.IPageCache;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 结算金额轻分析报表（个体）
 */

public class TlementAmountQingQueryPlugin extends AbstractFormPlugin implements IQingDataProvider {

    private static Log log = LogFactory.getLog(TlementAmountQingQueryPlugin.class);

    //收款单源单类型
    //private String[] recBillType = {"fca_transdownbill","fca_transupbill"};
    //付款单源单类型
    //private String[] payBillType = {"Qyhkd","Qyfxd","Qydkfkd"};
    //组织形态
    private String[] orgType = {"部门","工厂","集团","集团公司","事业部"};

    @Override
    public QingMeta getMeta(String s) {
        String entityKey = "cas_recbill";
        QingMeta meta = new QingMeta();
        EntryEntity entity1 = new EntryEntity();
        entity1.setKey(entityKey);
        entity1.setName(new LocaleString("收款处理"));
        entity1.setPkFieldName("id");
        meta.addEntryEntity(entity1);

        Field column = new Field();
        /*column.setEntity(entityKey);//字段所属实体key	否
        column.setKey("id");// 字段名(唯一标识)	是
        column.setFieldType(QingFieldType.Int.toNumber()); //数据类型	是
        column.setName(new LocaleString("id")); //字段别名	是
        meta.addColumn(column);*/

        column = new Field();
        column.setEntity(entityKey);//字段所属实体key	否
        column.setKey("inflow");// 字段名(唯一标识)	是
        column.setFieldType(QingFieldType.Number.toNumber()); //数据类型	是
        column.setName(new LocaleString("本月流入")); //字段别名	是
        meta.addColumn(column);

        column = new Field();
        column.setEntity(entityKey);
        column.setKey("outflow");
        column.setFieldType(QingFieldType.Number.toNumber());
        column.setName(new LocaleString("本月流出"));
        meta.addColumn(column);

        column = new Field();
        column.setEntity(entityKey);
        column.setKey("claninflow");
        column.setFieldType(QingFieldType.Number.toNumber());
        column.setName(new LocaleString("本月净流入"));
        meta.addColumn(column);

        /*column = new Field();
        column.setEntity(entityKey);
        column.setKey("bizdate");
        column.setFieldType(QingFieldType.DateTime.toNumber());
        column.setName(new LocaleString("日期"));
        meta.addColumn(column);*/

        /*column = new Field();
        column.setEntity(entityKey);
        column.setKey("orgName");
        column.setFieldType(QingFieldType.String.toNumber());
        column.setName(new LocaleString("组织"));
        meta.addColumn(column);*/
        return meta;
    }

    @Override
    public QingData getData(String s, int startRow, int i1) {
        String qFilter = getPageCache().get("qFilter");
        if (qFilter != null) {
            //通过此方法拿到过滤参数
            Map<String, Object> filterParam = (Map) JSON.parseObject(qFilter, Map.class);
        }
        QFilter[] billFilters = null;
        //单据状态=审核
        QFilter qFilter1 = new QFilter("billstatus", QCP.equals,"D");
        //结算方式为空
        QFilter settletnumberFilter = new QFilter("settletnumber", QCP.equals," ");

        List<String> orgNameList = new ArrayList<>();
        //查询辅助资料-股份下公司
        DynamicObject[] detail = BusinessDataServiceHelper.load("bos_assistantdata_detail", "id,name,group", new QFilter[]{new QFilter("group.id", QCP.equals, 2084821314140406784L)});
        if (detail.length > 0) {
            Arrays.asList(detail).forEach(d->{
                orgNameList.add(d.getString("name"));
            });
        }

        SimpleDateFormat strFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat endFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
//        cal.setTime(new Date());
        try {
            cal.setTime(endFormat.parse("2025-04-30 23:59:59"));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        cal.set(Calendar.DAY_OF_MONTH, 1);
        String formatData = null;
        Date oldDate = null;
        Date nowDate = null;
        try {
            formatData = strFormat.format(cal.getTime());
            formatData = formatData + " 00:00:00";
            oldDate = endFormat.parse(formatData);
//            nowDate = endFormat.parse(endFormat.format(new Date()));
            nowDate = endFormat.parse("2025-04-30 23:59:59");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        DynamicObject test = BusinessDataServiceHelper.newDynamicObject("nckd_testdocuments");
        test.set("billno","test-geti-" + new SimpleDateFormat("yyyyMMddHHmm").format(new Date()));
        test.set("billstatus","A");
        DynamicObjectCollection entryentity = test.getDynamicObjectCollection("entryentity");

        //查账户查询基础资料
        DataSet bankDataSet = QueryServiceHelper.queryDataSet(this.getClass().getName(), "am_accountbank", "id,openorg.name as opName,bankaccountnumber,acctstatus", null, null);

        //查辅助资料
        DataSet detailDataSet = QueryServiceHelper.queryDataSet(this.getClass().getName(), "bos_assistantdata_detail", "id,name,group", new QFilter[]{new QFilter("group.id", QCP.equals, 2084821314140406784L)}, null);


        /*
        收款单
         */
        QFilter qFilter2 = new QFilter("org.name", QCP.in,orgNameList);
        QFilter dateQf = new QFilter("bizdate", QCP.large_equals, oldDate).and(new QFilter("bizdate", QCP.less_equals, nowDate));
        billFilters = new QFilter[]{qFilter1,qFilter2,dateQf,settletnumberFilter};
        log.info("个体收款单查询条件 " + JSON.toJSONString(billFilters));
        String recBillFiled = "id,billstatus,billno,settletnumber,actrecamt,org,org.id as recOrgId,org.name as orgName,payeedate,bizdate,payeracctbanknum,sourcebilltype";
        DataSet recBillData = QueryServiceHelper.queryDataSet(this.getClass().getName(), "cas_recbill", recBillFiled, billFilters, null);
        DataSet recBillData1 = recBillData.join(bankDataSet).on("payeracctbanknum", "bankaccountnumber").select(new String[]{"id", "billstatus", "billno", "settletnumber", "actrecamt", "recOrgId", "orgName", "payeedate", "bizdate", "payeracctbanknum"}, new String[]{"bankaccountnumber", "opName"}).finish();
        recBillData1 = recBillData1.select("id", "billstatus", "billno", "settletnumber", "actrecamt", "recOrgId", "orgName", "payeedate", "bizdate", "payeracctbanknum","opName");
        recBillData1 = recBillData1.leftJoin(detailDataSet).on("opName","name").select(new String[]{"id", "billstatus", "billno", "settletnumber", "actrecamt", "recOrgId", "orgName", "payeedate", "bizdate", "payeracctbanknum","opName"},new String[]{"name"}).finish();
        recBillData1 = recBillData1.filter("name is null");
        DataSet recBillData2 = recBillData.leftJoin(bankDataSet).on("payeracctbanknum", "bankaccountnumber").select(new String[]{"id", "billstatus", "billno", "settletnumber", "actrecamt", "recOrgId", "orgName", "payeedate", "bizdate", "payeracctbanknum"}, new String[]{"bankaccountnumber", "opName"}).finish();
        recBillData2 = recBillData2.filter("bankaccountnumber is null").addField("null","name").select("id", "billstatus", "billno", "settletnumber", "actrecamt", "recOrgId", "orgName", "payeedate", "bizdate", "payeracctbanknum","opName","name");
        recBillData1 = recBillData1.union(recBillData2);
        recBillData1 = recBillData1.select("id,actrecamt,billno");

        /**
         * 收款单-业务处理
         */
        QFilter sourceFilter = new QFilter("sourcebilltype",QCP.equals,"cdm_drafttradebill");
        billFilters = new QFilter[]{qFilter1,qFilter2,dateQf, sourceFilter};
        DataSet sourceRecBillData = QueryServiceHelper.queryDataSet(this.getClass().getName(), "cas_recbill", recBillFiled, billFilters, null);
        recBillData1 = sourceRecBillData.select("id,actrecamt,billno").union(recBillData1).distinct();
        //打印单据
        DataSet recCopy = recBillData1.copy();
        while (recCopy.hasNext()) {
            DynamicObject entry = entryentity.addNew();
            Row row = recCopy.next();
            entry.set("nckd_name", "收款单");
            entry.set("nckd_number", row.get("billno"));
            entry.set("nckd_amount", row.get("actrecamt"));
        }


        /*StringBuffer recBillDataSetlog = new StringBuffer();
        DataSet newRecBillData = recBillData1.copy().groupBy().sum("actrecamt").finish();
        while (newRecBillData.hasNext()) {
            Row next = newRecBillData.next();
            recBillDataSetlog.append(next.getBigDecimal("actrecamt"));
        }
        log.info("---收款单总额--- " + recBillDataSetlog);*/

        DataSet groupRecBillData = recBillData1.groupBy().sum("actrecamt").finish();
        groupRecBillData = groupRecBillData.select("actrecamt,0 as actpayamt,0 as actpayamount");

        /*
        付款单
         */
        QFilter channelFilter = new QFilter("paymentchannel", QCP.equals, "bei");
        dateQf = new QFilter("paydate", QCP.large_equals, oldDate).and(new QFilter("paydate", QCP.less_equals, nowDate));
        billFilters = new QFilter[]{qFilter1,qFilter2,dateQf,settletnumberFilter,channelFilter};
        log.info("个体付款单1查询条件 " + JSON.toJSONString(billFilters));
        String payBillFiled = "id,billno,billstatus,settletnumber,org,org.id as payOrgId,org.name as orgName,paydate,actpayamt,payeebanknum,paymentchannel,bizdate,sourcebilltype";
        DataSet renPayBillData1 = QueryServiceHelper.queryDataSet(this.getClass().getName(), "cas_paybill", payBillFiled, billFilters, null);
        DataSet payBillData1 = renPayBillData1.join(bankDataSet).on("payeebanknum", "bankaccountnumber").select(new String[]{"id","billno", "actpayamt", "payeebanknum"}, new String[]{"bankaccountnumber", "opName"}).finish();
        payBillData1 = payBillData1.select("id","billno", "actpayamt", "payeebanknum","opName");
        payBillData1 = payBillData1.leftJoin(detailDataSet).on("opName","name").select(new String[]{"id","billno", "actpayamt", "payeebanknum","opName"},new String[]{"name"}).finish();
        payBillData1 = payBillData1.filter("name is null");
        DataSet payBillData3 = renPayBillData1.leftJoin(bankDataSet).on("payeebanknum", "bankaccountnumber").select(new String[]{"id","billno", "actpayamt", "payeebanknum"}, new String[]{"bankaccountnumber", "opName"}).finish();
        payBillData3 = payBillData3.filter("bankaccountnumber is null").addField("null","name").select("id","billno", "actpayamt", "payeebanknum","opName","name");
        payBillData1 = payBillData1.union(payBillData3);
        payBillData1 = payBillData1.select("id,actpayamt,billno");

        /*
        付款单
         */
        channelFilter = new QFilter("paymentchannel", QCP.equals, "onlinebank").or(new QFilter("paymentchannel", QCP.equals, "counter"));
        dateQf = new QFilter("bizdate", QCP.large_equals, oldDate).and(new QFilter("bizdate", QCP.less_equals, nowDate));
        billFilters = new QFilter[]{qFilter1,qFilter2,dateQf,settletnumberFilter,channelFilter};
        log.info("个体付款单2查询条件 " + JSON.toJSONString(billFilters));
        DataSet renPayBillData2 = QueryServiceHelper.queryDataSet(this.getClass().getName(), "cas_paybill", payBillFiled, billFilters, null);
        DataSet payBillData2 = renPayBillData2.join(bankDataSet).on("payeebanknum", "bankaccountnumber").select(new String[]{"id","billno", "actpayamt", "payeebanknum"}, new String[]{"bankaccountnumber", "opName"}).finish();
        payBillData2 = payBillData2.select("id","billno", "actpayamt", "payeebanknum","opName");
        payBillData2 = payBillData2.leftJoin(detailDataSet).on("opName","name").select(new String[]{"id","billno", "actpayamt", "payeebanknum","opName"},new String[]{"name"}).finish();
        payBillData2 = payBillData2.filter("name is null");
        DataSet payBillData4 = renPayBillData2.leftJoin(bankDataSet).on("payeebanknum", "bankaccountnumber").select(new String[]{"id","billno", "actpayamt", "payeebanknum"}, new String[]{"bankaccountnumber", "opName"}).finish();
        payBillData4 = payBillData4.filter("bankaccountnumber is null").addField("null","name").select("id","billno", "actpayamt", "payeebanknum","opName","name");
        payBillData2 = payBillData2.union(payBillData4);
        payBillData2 = payBillData2.select("id,actpayamt,billno");

        /**
         * 付款单-业务处理
         */
        billFilters = new QFilter[]{qFilter1,qFilter2,dateQf,sourceFilter};
        DataSet payBillData5 = QueryServiceHelper.queryDataSet(this.getClass().getName(), "cas_paybill", payBillFiled, billFilters, null);
        DataSet payBillData = payBillData5.select("id,actpayamt,billno").union(payBillData1).union(payBillData2).distinct();

        //打印单据
        DataSet payCopy3 = payBillData.copy();
        while (payCopy3.hasNext()) {
            DynamicObject entry = entryentity.addNew();
            Row row = payCopy3.next();
            entry.set("nckd_name", "付款单");
            entry.set("nckd_number", row.get("billno"));
            entry.set("nckd_amount", row.get("actpayamt"));
        }

        payBillData = payBillData.groupBy().sum("actpayamt").finish().select("0 as actrecamt,actpayamt,0 as actpayamount");

        /*StringBuffer payBillDataSetlog = new StringBuffer();
        DataSet newPayBillData = payBillData.copy().groupBy().sum("actpayamt").finish();
        while (newPayBillData.hasNext()) {
            Row next = newPayBillData.next();
            payBillDataSetlog.append(next.getBigDecimal("actpayamt"));
        }
        log.info("---付款单总额--- " + payBillDataSetlog);*/


        /*
        代发单
         */
        String agentBillFiled = "id,billno,billstatus,actpayamount,org,org.id as agentOrgId,org.name as orgName,paytime";
        dateQf = new QFilter("paytime", QCP.large_equals, oldDate).and(new QFilter("paytime", QCP.less_equals, nowDate));
        billFilters = new QFilter[]{qFilter1,qFilter2,dateQf,settletnumberFilter};
        log.info("个体代发单查询条件 " + JSON.toJSONString(billFilters));
        DataSet agentBillData = QueryServiceHelper.queryDataSet(this.getClass().getName(), "cas_agentpaybill", agentBillFiled, billFilters, null);

        //打印单据
        DataSet agentCopy = agentBillData.copy();
        while (agentCopy.hasNext()) {
            DynamicObject entry = entryentity.addNew();
            Row row = agentCopy.next();
            entry.set("nckd_name", "代发单");
            entry.set("nckd_number", row.get("billno"));
            entry.set("nckd_amount", row.get("actpayamount"));
        }
        SaveServiceHelper.save(new DynamicObject[]{test});
        agentBillData = agentBillData.groupBy().sum("actpayamount").finish().select("0 as actrecamt,0 as actpayamt,actpayamount");

        DataSet dataSet2 = groupRecBillData.union(payBillData).union(agentBillData);

        QingData data = null;
        if (startRow < 200) {
            data = new QingData();
            Map<String, Integer> dataindex = new HashMap<String, Integer>();
            dataindex.put("inflow", 0);
            dataindex.put("outflow", 1);
            dataindex.put("claninflow", 2);
            data.setDataindex(dataindex);

            List<Object[]> rows = new ArrayList<Object[]>();
            while (dataSet2.hasNext()) {
                Row row = dataSet2.next();
                Object[] obj = new Object[5];
                BigDecimal actrecamt = row.getBigDecimal("actrecamt") == null ? BigDecimal.ZERO : row.getBigDecimal("actrecamt");
                BigDecimal actpayamt = row.getBigDecimal("actpayamt") == null ? BigDecimal.ZERO : row.getBigDecimal("actpayamt");
                BigDecimal actpayamount = row.getBigDecimal("actpayamount") == null ? BigDecimal.ZERO : row.getBigDecimal("actpayamount");
                obj[0] = actrecamt;
                obj[1] = actpayamt.add(actpayamount);
                obj[2] = actrecamt.subtract(actpayamt.add(actpayamount));
                rows.add(obj);
            }
            data.setRows(rows);
        }
        return data;
    }

    @Override
    public void setFilterParameter(String filterParameter) {
        IPageCache pageCache = getPageCache();
        pageCache.put("qFilter", filterParameter);

    }

    @Override
    public String getFilterParameter() {
        return getPageCache().get("qFilter");
    }


}
